﻿using System;
using System.Collections;
using System.Collections.Generic;
using GameFramework.Fsm;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityGameFramework.Runtime;

namespace StarForce {
    public class CardIdleState : BasicCardState
    {
        private Vector3 DefaultScale { get; set; }
        public CardIdleState(Card handler,UiCardParameters parameters) : base(handler,parameters) {
            DefaultScale = handler.transform.localScale;
        }

        protected override void OnEnter(IFsm<Card> fsm) {
            base.OnEnter(fsm);
            Hander.Input.OnPointerDown += OnPointerDown;
            Hander.Input.OnPointerEnter += OnPointerEnter;
            Hander.Hand.OnCardSelect += OnSelectCard;
            if (Hander.Movement.IsOperating) {
                DisableCollision();
                Hander.Movement.OnFinishMotion += Enable;
            }
            else {
                Enable();
            }
            MakeRenderNormal();
            Hander.ScaleTo(DefaultScale,Parameters.ScaleSpeed);
        }

        protected override void OnLeave(IFsm<Card> fsm, bool isShutdown) {
            Hander.Input.OnPointerDown -= OnPointerDown;
            Hander.Input.OnPointerEnter -= OnPointerEnter;
            Hander.Hand.OnCardSelect -= OnSelectCard;
            Hander.Movement.OnFinishMotion -= Enable;
            base.OnLeave(fsm, isShutdown);
        }

        void OnPointerEnter(PointerEventData eventData){
            // 鼠标悬浮在这个卡牌上
            if (Fsm.CurrentState == this) {
                ChangeState<CardHoverState>(Fsm);
            }
        }

        public void OnPointerDown(PointerEventData eventData) {
            // 鼠标点击选中这张卡牌
            if (Fsm.CurrentState == this) {
                ChangeState<CardSelectState>(Fsm);
            }
        }

        public void OnSelectCard(Card card) {
            if (card != this.Hander) {
                ChangeState<CardDiableState>(Fsm);
            }
        }
    }
}

